--require "MainScene"

ZhuangBeiLayer3={}
local NodeID = {ArmyTable=1001, BagTable=1002, HeadBK=1003}
local EquipPanelID = {TouKui=10001, YiFu=10002, WuQi=10001, PiFeng=10002, XieZi=10004, TeShu=10003}
local HeroItemID = {ImgBK=1001, Name=1002, Level=1003, BingZhong=1004}

ZhuangBeiLayer3.equipButtons = Array:create() 
ZhuangBeiLayer3.headImg = nil
ZhuangBeiLayer3.equipButtonsInBag = Array:create() 
ZhuangBeiLayer3.bagTable = nil
ZhuangBeiLayer3.bagItems = nil
ZhuangBeiLayer3.selectedType = nil
ZhuangBeiLayer3.currentHeroID = nil
ZhuangBeiLayer3.currentSelectedEquipIndex = nil

ZhuangBeiLayer3.getNode = function()
  ZhuangBeiLayer3.node = CCBReader:load("ZhuangBeiLayer3.ccbi")
  print("ZhuangBeiLayer3.getNode", ZhuangBeiLayer3.node)
  ZhuangBeiLayer3.init() 
  return ZhuangBeiLayer3.node
end

ZhuangBeiLayer3.getScene = function()
  local sc = CCScene:create()
  sc:addChild(ZhuangBeiLayer3.getNode())  
  print("ZhuangBeiLayer3.getScene", sc)
  return sc
end

ZhuangBeiLayer3.clear = function()
  ZhuangBeiLayer3.equipButtonsInBag = Array:create()
  ZhuangBeiLayer3.equipButtons = Array:create()
  ZhuangBeiLayer3.headImg = nil
  ZhuangBeiLayer3.bagTable = nil
  ZhuangBeiLayer3.bagItems = nil
  ZhuangBeiLayer3.selectedType = nil
  ZhuangBeiLayer3.currentHeroID = nil
end

ZhuangBeiLayer3.init = function()
  ZhuangBeiLayer3.clear()
  ZhuangBeiLayer3.initArmygroupTable()
  ZhuangBeiLayer3.initBagTable()
  ZhuangBeiLayer3.showEquipInTable()
  ZhuangBeiLayer3.currentHeroID = 1
  ZhuangBeiLayer3.showEquip()
  --ZhuangBeiLayer3.node:registerScriptTouchHandler(onTouchLayer)
  --ZhuangBeiLayer3.node:setTouchEnabled(true)
end

ZhuangBeiLayer3.showEquipInTable = function()
  
  for i=0,ZhuangBeiLayer3.equipButtonsInBag:size()-1 do
    local b = ZhuangBeiLayer3.equipButtonsInBag:at(i)
    if b~=nil then
      b:getParent():removeChild(b,true)
    end
  end
  ZhuangBeiLayer3.equipButtonsInBag = Array:create()
  local equipIDs = UserData.userinfo.host.equips
  for k,v in pairs(equipIDs) do
    if v.p >= 0 then
      local index = math.floor(v.p/4)
      local index1 = v.p%4
      print("ZhuangBeiLayer3.showEquipInTable", k, v, index,index1) 
      local item = ZhuangBeiLayer3.bagItems:objectAtIndex(ZhuangBeiLayer3.bagItems:count()-index-1)
      local einfo = UserData.config.models.Equip[v.e]
      local imgName = nameFromPath(einfo.asset)
      local button = CCButton:createWithFileName("onSelectEquipOff", imgName, imgName, imgName)
      button:setTag(3000+k)
      local bkSP = item:getChildByTag(1001+index1)  
      button:setPosition(bkSP:getPosition())
      item:addChild(button)
      ZhuangBeiLayer3.equipButtonsInBag:append(button)
    end
  end 
end

ZhuangBeiLayer3.initBagTable = function()
  local items = CCArray:create()
  for i=0,10 do
    local item = CCBReader:load("BagItem.ccbi")
    item:setContentSize(CCSizeMake(430,100))
    items:addObject(item)  
  end
  local tableBK = ZhuangBeiLayer3.node:getChildByTag(NodeID.BagTable)
  local size = tableBK:getContentSize()
  local tableview = CCTableView:create(1, CCSizeMake(size.width, size.height), items)
  print("ZhuangBeiLayer3.initBagTable", tableview)
  local x, y = ActionUtil.leftBottomPoint(tableBK)
  tableview:setPosition(x, y)
  ZhuangBeiLayer3.node:addChild(tableview)
  --ZhuangBeiLayer3.bagTable = tableview
  ZhuangBeiLayer3.bagItems = items
end

ZhuangBeiLayer3.initArmygroupTable = function()
  local armys = UserData.userinfo.host.armyGroups
  local items = CCArray:create()
  for k,v in pairs(armys) do
    print("initArmygroupTable", k, v)
    local item = CCBReader:load("HeroItem.ccbi")
    local info = UserData.config.models.General[tonumber(k)]
    --local sp = CCSprite:create(string.format("%s.png", info.asset))
    local imgName = string.format("%d.png",k%40)
    local button = CCButton:createWithFileName("onHeroButton", imgName, imgName, imgName)
    local imgBK = item:getChildByTag(HeroItemID.ImgBK)
    button:setPosition(imgBK:getPosition())
    button:setTag(k)
    item:addChild(button)

    local lb = item:getChildByTag(HeroItemID.Name) 
    TypeCast(lb, "CCLabelTTF"):setString(info.name)
    if info.quality=="green" then
      lb:setColor(ccc3(0,255,0))
    elseif info.quality=="blue" then
      lb:setColor(ccc3(0,0,255))
    else
      lb:setColor(ccc3(255,255,255))
    end
    lb = item:getChildByTag(HeroItemID.BingZhong)
    TypeCast(lb, "CCLabelTTF"):setString(info.desc)
    lb = item:getChildByTag(HeroItemID.Level)
    TypeCast(lb, "CCLabelTTF"):setString(string.format("Lv%d", v.l))
   
    item:setContentSize(CCSizeMake(300,127)) 
    items:addObject(item)
  end
  print("ZhanDouScene.initStrongholdTable items", items:count())
  local tableBK = ZhuangBeiLayer3.node:getChildByTag(NodeID.ArmyTable)
  local size = tableBK:getContentSize()
  local tableview = CCTableView:create(0, CCSizeMake(size.width-20, size.height), items)
  local x, y = ActionUtil.leftBottomPoint(tableBK)
  tableview:setPosition(x+10, y)
  ZhuangBeiLayer3.node:addChild(tableview)
end

ZhuangBeiLayer3.clearEquip = function()

end

ZhuangBeiLayer3.showEquip = function()
  local heroid = ZhuangBeiLayer3.currentHeroID 
  print("ZhuangBeiLayer3.showEquip", heroid)

  --设置头像
  if ZhuangBeiLayer3.headImg~=nil then
    ZhuangBeiLayer3.node:removeChild(ZhuangBeiLayer3.headImg,true)
  end 
  local spBK = ZhuangBeiLayer3.node:getChildByTag(NodeID.HeadBK)
  local sp = CCSprite:create(string.format("%d.png", heroid%40))
  sp:setPosition(spBK:getPosition())
  ZhuangBeiLayer3.node:addChild(sp)
  ZhuangBeiLayer3.headImg = sp
 
  --设置装备
  for i=0,ZhuangBeiLayer3.equipButtons:size()-1 do
    local b = ZhuangBeiLayer3.equipButtons:at(i)
    if b~=nil then
      ZhuangBeiLayer3.node:removeChild(b,true)
    end
  end
  ZhuangBeiLayer3.equipButtons = Array:create()
  local equipIDs = UserData.userinfo.host.equips
  local equipIndexs = UserData.userinfo.host.armyGroups[tostring(heroid)].e
  for k,v in ipairs(equipIndexs) do
    print("ZhuangBeiLayer3.showEquip", k, v)
    if v>0 then
      local eid = equipIDs[tostring(v)].e
      local einfo = UserData.config.models.Equip[eid]
      local imgName = nameFromPath(einfo.asset)
      print("ZhuangBeiLayer3.showEquip", v, eid, imgName)
      local bkImg = ZhuangBeiLayer3.node:getChildByTag(10000+k)
      local button = CCButton:createWithFileName("onSelectEquipOn", imgName, imgName, imgName)
      button:setTag(k)
      button:setPosition(bkImg:getPosition()) 
      ZhuangBeiLayer3.node:addChild(button)
      ZhuangBeiLayer3.equipButtons:append(button)
    end
  end 
end

function onHeroButton(node)
  local tag = node:getTag()
  print("onHeroButton", tag) 
  ZhuangBeiLayer3.currentHeroID = tag 
  ZhuangBeiLayer3.showEquip()
end

ZhuangBeiLayer3.createTip = function(eIndex)
  local info1 = UserData.userinfo.host.equips[tostring(eIndex)] 
  local info2 = UserData.config.models.Equip[info1.e]
  
  local tipLayer = CCBReader:load("EquipProperty.ccbi")
  local spBK = tipLayer:getChildByTag(1001)
  local sp = CCSprite:create(nameFromPath(info2.asset)) 
  sp:setPosition(spBK:getPosition())
  tipLayer:addChild(sp)

  local lb = tipLayer:getChildByTag(1002)
  TypeCast(lb, "CCLabelTTF"):setString(info2.name)
  
  lb = tipLayer:getChildByTag(1010)
  if ZhuangBeiLayer3.selectedType==0 then 
    TypeCast(lb, "CCLabelTTF"):setString("装备")
  else
    TypeCast(lb, "CCLabelTTF"):setString("卸下")
  end

  return tipLayer
end

function onSelectEquipOff(node, event)
  ZhuangBeiLayer3.selectedType = 0 
  local equipIDs = UserData.userinfo.host.equips
  local index = node:getTag()-3000
  ZhuangBeiLayer3.currentSelectedEquipIndex = index 
  print("onSelectEquipOff", index)
  local info1 = equipIDs[tostring(index)]
  local info2 = UserData.config.models.Equip[info1.e]
  if event==CCControlEventTouchUpInside then
    local imgName = nameFromPath(info2.asset)
    print("onSelectEquipOff", event, imgName)
    ZhuangBeiLayer3.node:addChild(ZhuangBeiLayer3.createTip(index))
  end
end
function onSelectEquipOn(node, event)
  ZhuangBeiLayer3.selectedType = 1 
  local equipIDs = UserData.userinfo.host.equips
  local equipIndexs = UserData.userinfo.host.armyGroups[tostring(ZhuangBeiLayer3.currentHeroID)].e
  local index = node:getTag()
  local eid = equipIDs[tostring(equipIndexs[index])].e
  local einfo = UserData.config.models.Equip[eid]
  ZhuangBeiLayer3.currentSelectedEquipIndex = equipIndexs[index] 
  if event==CCControlEventTouchUpInside then
    local imgName = nameFromPath(einfo.asset)
    print("onSelectEquipOn", event, imgName)
    ZhuangBeiLayer3.node:addChild(ZhuangBeiLayer3.createTip(equipIndexs[index]))
  end
end
function onEquipButton(node)
  if ZhuangBeiLayer3.selectedType==1 then
    NetworkRequest.takeOff(ZhuangBeiLayer3.currentHeroID, ZhuangBeiLayer3.currentSelectedEquipIndex)
  else
    NetworkRequest.equip(ZhuangBeiLayer3.currentHeroID, ZhuangBeiLayer3.currentSelectedEquipIndex)
  end
  node:getParent():getParent():removeChild(node:getParent(), true)
end
function onSellButton(node)
  local equipIDs = UserData.userinfo.host.equips
  NetworkRequest.batchSell(equipIDs[tostring(ZhuangBeiLayer3.currentSelectedEquipIndex)].p)
  node:getParent():getParent():removeChild(node:getParent(), true)
end
--function onCloseTip(node)
--  node:getParent():getParent():removeChild(node:getParent(), true)
--end
